package com.soft.small.util.http;

import com.soft.util.decrypt.DecryptResponse;
import com.soft.util.encrypt.Aes;
import com.soft.util.encrypt.Md5;
import com.soft.util.encrypt.SignCore;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SmallHttp {
    private String appKey;
    private String appSecret;
    private String gateway ;

    public SmallHttp(String appKey, String appSecret, String gateway){
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.gateway = gateway;
    }

    /**
     * 请求头的生成
     * @param method
     * @param data
     * @param token
     * @return
     */
    private Map<String, String> getHeader(String method,String data,String token){
        Map<String, String> headerMap = new HashMap<>();
        Long timestamp = System.currentTimeMillis();
        if (token != null){
            headerMap.put("token", token);
        }
        headerMap.put("appkey", appKey);
        headerMap.put("method", method);
        headerMap.put("timestamp", timestamp+"");
        headerMap.put("nonce", "11111");
        headerMap.put("signType", "md5");
        headerMap.put("encryptType", "AES");
        headerMap.put("version", "1.0");
        headerMap.put("isEncrypt", "true");
        if (data != null){
            headerMap.put("data", data);
        }
        String sign = Md5.sign(SignCore.createLinkString(headerMap),appSecret, "");
        headerMap.remove("data");
        headerMap.put("sign",sign);
        headerMap.put("Content-Type","application/json");
        return headerMap;
    }

    /**
     * 发送post请求
     * @param method
     * @param unEncryptData
     * @param token
     * @return
     */
    public String requestPost(String method, String unEncryptData, String token){
        log.info("{}接口的发送数据为：\n------{}",method,unEncryptData);
        String encryptData = Aes.encryptAes(unEncryptData,appSecret);
        log.debug("加密后的数据为：{}",encryptData);
        Map<String, String> headerMap = getHeader(method,encryptData,token);
        log.debug("请求头为：{}",headerMap.toString());
        String result = HttpUtil.requestPost(gateway,headerMap,encryptData);
        log.debug("返回结果未解密：{}",result);
        String resultDecrypt = DecryptResponse.parseResponse(result,appSecret);
        log.info("{}接口的返回结果为：\n ------{}",method,resultDecrypt);
        return resultDecrypt;
    }

    public String requestGet(String method, String unEncryptData, String token){
        String gatewayNew;
        String encryptData = null;
        if (unEncryptData != null){
            log.info("{}接口的发送数据为：\n------{}",method,unEncryptData);
            encryptData = Aes.encryptAes(unEncryptData,appSecret);
            log.debug("加密后的数据为：{}",encryptData);
            gatewayNew = gateway + "?data="+encryptData;
        }else{
            gatewayNew = gateway;
        }
        Map<String, String> headerMap = getHeader(method,encryptData,token);
        String result = HttpUtil.requestGet(gatewayNew,headerMap);
        log.debug("返回结果未解密：{}",result);
        String resultDecrypt = DecryptResponse.parseResponse(result,appSecret);
        log.info("{} 返回结果解密后为：\n------{}",method,resultDecrypt);
        return resultDecrypt;
    }
}
